package com.hww.gannan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hww.gannan.dao.DetailsDao;
import com.hww.gannan.dao.FriendlyLinkDao;
import com.hww.gannan.dao.IndexDao;
import com.hww.gannan.dao.MessageDao;
import com.hww.gannan.entity.Details;
import com.hww.gannan.entity.FriendlyLink;
import com.hww.gannan.entity.Message;
import com.hww.gannan.entity.SysLogin;
import com.hww.gannan.service.IndexService;
import com.hww.gannan.util.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class IndexServiceImpl implements IndexService {
    @Resource
    private MessageDao messageDao;
    @Resource
    private IndexDao indexDao;
    @Resource
    private FriendlyLinkDao friendlyLinkDao;
    @Resource
    private DetailsDao detailsDao;

    @Override
    public Result addMessage(Message message) {
        message.setCreateTime(new Date());
        int row = messageDao.insert(message);
        if (row > 0) {
            return Result.success(null);
        }
        return Result.error(null);
    }

    @Override
    public Result addSysLogin(SysLogin sysLogin) {
        Long row = indexDao.selectCount(new QueryWrapper<SysLogin>().eq("username", sysLogin.getUsername()));
        if (row > 0) {
            return Result.error(null);
        }
        indexDao.insert(sysLogin);
        return Result.success(null);
    }

    @Override
    public Result login(SysLogin sysLogin) {
        Long count = indexDao.selectCount(new QueryWrapper<SysLogin>().eq("username", sysLogin.getUsername()).eq("password", sysLogin.getPassword()));
        if (count > 0) {
            return Result.success(null);
        }
        return Result.error(null);
    }

    @Override
    public Result getUrlList() {
        List<FriendlyLink> list = friendlyLinkDao.selectList(null);
        return Result.success(list);
    }

    @Override
    public Result getDetails(Integer id) {
        Details details = detailsDao.selectById(id);
        return Result.success(details);
    }

}
